//
//  NewAndFontCell.swift
//  Eternity
//
//  Created by cll12_12 on 2016/12/2.
//  Copyright © 2016年 ZhengWei. All rights reserved.
//

import UIKit

enum NINELIST {
    case NEWLIST
    case ADVONE
    case ADVTWO
    case FONDLIST
}

class NewAndFontCell: UICollectionViewCell {

    var isLeft: Bool?
    var backgroundViews: [UIView]?
    var imageViews: [UIImageView]?
    var brandLabels: [UILabel]?
    var productLabels: [UILabel]?
    var priceLabels: [UILabel]?
    
    // MARK: - life cycle
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        backgroundViews = [UIView]()
        imageViews = [UIImageView]()
        brandLabels = [UILabel]()
        productLabels = [UILabel]()
        priceLabels = [UILabel]()
        
        initBaseLayout()
        layoutPageSubviews()
        createAndLayoutViews()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func apply(_ layoutAttributes: UICollectionViewLayoutAttributes) {
    }
    
    // MARK: - private method
    func initBaseLayout() {
        self.backgroundColor = UIColor.white
        self.addSubview(scrollView)
    }
    
    func layoutPageSubviews() {
        scrollView.snp.makeConstraints { (make) in
            make.left.equalTo(self).offset(0)
            make.top.equalTo(self).offset(0)
            make.width.equalTo(SCREEN_WIDTH)
            make.height.equalTo(195)
        }
    }
    
    func createAndLayoutViews() {
        let width = (SCREEN_WIDTH-24)/3
        let height = (SCREEN_WIDTH-24)/3
        for i in 0 ..< 3 {
            var index = i + 1
            if index == 3 {
                index = 0
            }
            let backGroundView = UIView(frame: CGRect(x: SCREEN_WIDTH*CGFloat(index), y: 0, width: SCREEN_WIDTH, height: 175))
            self.backgroundViews?.append(backGroundView)
            self.scrollView.addSubview(backGroundView)
            
            for j in 0 ..< 3 {
                let imageView = UIImageView(frame: CGRect(x: (width+8)*CGFloat(j)+4, y: 0, width: width, height: height))
                self.imageViews?.append(imageView)
                backGroundView.addSubview(imageView)
                
                let brandLabel = UILabel(frame: CGRect(x: (width+8)*CGFloat(j)+4, y: height+10, width: width, height: 13))
                brandLabel.font = UIFont(name: "PingFangSC-Regular", size: 12)
                brandLabel.textColor = UIColor(hexString: "#252525")
                self.brandLabels?.append(brandLabel)
                backGroundView.addSubview(brandLabel)
                
                let productLabel = UILabel(frame: CGRect(x: (width+8)*CGFloat(j)+4, y: height+23+4, width: width, height: 13))
                productLabel.font = UIFont(name: "PingFangSC-Light", size: 12)
                productLabel.textColor = UIColor(hexString: "#666666")
                self.productLabels?.append(productLabel)
                backGroundView.addSubview(productLabel)
                
                let priceLabel = UILabel(frame: CGRect(x: (width+8)*CGFloat(j)+4, y: height+36+4+8, width: width, height: 12))
                priceLabel.font = UIFont(name: "PingFangSC-Regular", size: 12)
                priceLabel.textColor = UIColor(hexString: "#252525")
                self.priceLabels?.append(priceLabel)
                backGroundView.addSubview(priceLabel)
            }
        }
    }
    
    func configUI(nineList: NINELIST, list: [ListOfGoodsModel]) {
//        let imageNameArr = ["http://www.2cto.com/uploadfile/Collfiles/20161109/201611091031451109.jpg", "http://img4.duitang.com/uploads/item/201506/24/20150624155647_JYFWM.thumb.700_0.jpeg", "http://www.2cto.com/uploadfile/Collfiles/20161109/201611091031451104.jpg", "http://c.hiphotos.baidu.com/zhidao/pic/item/21a4462309f79052eaddb7400af3d7ca7acbd58a.jpg", "http://www.2cto.com/uploadfile/Collfiles/20161109/20161109103103265.jpg", "http://www.2cto.com/uploadfile/Collfiles/20161109/20161109103123684.jpg", "http://www.2cto.com/uploadfile/Collfiles/20161109/201611091031451106.jpg", "http://img1.2345.com/duoteimg/qqTxImg/2/1893fe51cf7dc8c08d247abbb58e505b.jpg", "http://img4q.duitang.com/uploads/item/201506/24/20150624154310_3WL4E.jpeg"]
        
        for i in 0 ..< 9 {
            let imageView = self.imageViews?[i]
            let brandLabel = self.brandLabels?[i]
            let productLabel = self.productLabels?[i]
            let priceLabel = self.priceLabels?[i]
            
            imageView?.sd_setImage(with: URL(string: list[i].mainPicUrl!), placeholderImage: UIImage(named: "def_162x162"))
            
            brandLabel?.text = list[i].brandName
            productLabel?.text = list[i].title
            priceLabel?.text = "￥" + "\(list[i].rmbPrice!)"
        }
        
        switch nineList {
        case .NEWLIST:
            
            break
        case .ADVONE:
            break
        case .ADVTWO:
            break
        case .FONDLIST:
            break
        default:
            break
        }
    }
    
    func updatePosition(isLeft: Bool) {
        for backView in self.backgroundViews! {
            var frame = backView.frame
            if isLeft {
                frame.origin.x = frame.origin.x == SCREEN_WIDTH*2 ? 0 : (frame.origin.x + SCREEN_WIDTH)
            } else if !isLeft {
                frame.origin.x = frame.origin.x == 0 ? SCREEN_WIDTH*2 : (frame.origin.x - SCREEN_WIDTH)
            }
            backView.frame = frame
        }
        scrollView.contentOffset = CGPoint(x: SCREEN_WIDTH, y: 0)
    }
    
    // MARK: - setters and getters
    var _scrollView: UIScrollView!
    var scrollView: UIScrollView {
        if _scrollView == nil {
            _scrollView = UIScrollView()
            _scrollView.contentSize = CGSize(width: SCREEN_WIDTH*3, height: 195)
            _scrollView.contentOffset = CGPoint(x: SCREEN_WIDTH, y: 0)
            _scrollView.isPagingEnabled = true
            _scrollView.delegate = self
            _scrollView.showsHorizontalScrollIndicator = false
        }
        return _scrollView
    }
}

extension NewAndFontCell: UIScrollViewDelegate {
    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        
        let pageIndex = scrollView.contentOffset.x / self.frame.size.width
        
        if pageIndex == 2 { // 右滑
            updatePosition(isLeft: false)
        } else if pageIndex == 0 { // 左滑
            updatePosition(isLeft: true)
        }
        
    }
}
